var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressData:"",
    state:{
      province: false,
      city: false,
      area: false
    },
    val:{
      province: null
    },
   
    actArea: ["", "", ""],
  },

 
  onLoad: function (e) {
   
    var that = this;
    that.setData({
      wxaddressstatus:false
    })
    
    var id1 = e.id;
    if (id1) {
      // 初始化原数据
      wx.showLoading();
      const db = wx.cloud.database()
      db.collection('useraddress').where({
        _id: id1
      }).get({
        success: function (res) {
       console.log(res)
          wx.hideLoading();
          if (res.data.length > 0) {
            
            that.setData({
              updateid: res.data[0]._id,
              addressData: res.data[0],
              //state: { province: res.data[0].provinceStr, city: res.data[0].cityStr,
              //  area:res.data[0].districStr},
              actArea:[res.data[0].provinceId,res.data[0].cityId,res.data[0].districtId],
              ['val.area.province.value']: res.data[0].provinceStr,
               
                ['val.area.city.value']:res.data[0].cityStr,
                ['val.area.area.value']:res.data[0].districStr
            });
            
            return;
          } else {
            wx.showModal({
              title: '提示',
              content: '无法获取快递地址数据',
              showCancel: false
            })
          }
        }
      })
      
    }
  },
    //保存
    bindSave: function (e) {
      
      console.log(e)
      var that = this;
      var linkMan = e.detail.value.linkMan;
      var address = e.detail.value.address;
      var mobile = e.detail.value.mobile;
      
  
      if (linkMan == "") {
        wx.showModal({
          title: '提示',
          content: '请填写联系人姓名',
          showCancel: false
        })
        return
      }
      if (mobile == "") {
        wx.showModal({
          title: '提示',
          content: '请填写手机号码',
          showCancel: false
        })
        return
      }
     
      if (address == "") {
        wx.showModal({
          title: '提示',
          content: '请填写详细地址',
          showCancel: false
        })
        return
      }
    
     that.setData({
      ['addressData.linkMan']:linkMan,
      ['addressData.address']:address,
      ['addressData.mobile']:mobile,
      ['addressData.dateAdd']:new Date(),
      
     })
      const db = wx.cloud.database()
      if(!that.data.updateid){
          db.collection('useraddress').add({
            data: {
             
             
              provinceId: that.data.addressData.provinceId,
              provinceStr:  that.data.addressData.provinceStr,
            
              cityId:  that.data.addressData.cityId,
              cityStr:  that.data.addressData.cityStr,
              districtId: that.data.addressData.districtId,
              districStr: that.data.addressData.districStr,
                
              linkMan: linkMan,
              address: address,
              mobile: mobile,
              dateAdd:new Date(),
            
            },
            success: function(res) {
             
              wx.navigateBack({})
            }
          })
     }else{
      
      
      db.collection('useraddress').doc(that.data.updateid).update({
        data: {
              provinceId: that.data.addressData.provinceId,
              provinceStr:  that.data.addressData.provinceStr,
              cityId:  that.data.addressData.cityId,
              cityStr:  that.data.addressData.cityStr,
              districtId: that.data.addressData.districtId,
              districStr: that.data.addressData.districStr,
                
              linkMan: linkMan,
              address: address,
              mobile: mobile,
        
        
        },
        success: function(res) {
          console.log(res)
          wx.navigateBack()
        }
      })
     }
    
    },
  //省市区选择
  inToggle(e){
    
    const me = this;
    const getData = e.currentTarget.dataset;
    const { state } = me.data;
    let actState, setState;
    switch (getData.type) {
      //省
      case 'province':
        actState = 'state.province';
        setState = state.province;
        break;

      //市
      case 'city':
        actState = 'state.city';
        setState = state.city;
        break;

      //区
      case 'area':
        actState = 'state.area';
        setState = state.area;
        break;
    }
    me.setData({
      [actState]: !setState
    })
    

    //确定
    if (e.type == 'enter'){
      const getData = e.currentTarget.dataset;
      let actVal
      switch (getData.type){
        //省
        case 'province':
          actVal = 'val.province';
        break;

        //市
        case 'city':
          actVal = 'val.city';
        break;

        //区
        case 'area':
          actVal = 'val.area';
        break;
      }
      me.setData({
        [actVal]: e.detail,
        ['addressData.provinceId']:e.detail.province.id,
        ['addressData.cityId']:e.detail.city.id,
        ['addressData.districtId']:e.detail.area.id,
        ['addressData.provinceStr']:e.detail.province.value,
        ['addressData.cityStr']:e.detail.city.value,
        ['addressData.districStr']:e.detail.area.value,
      })
      console.log(e.detail)
      console.log(me.data.addressData)
    }
  },
  bindCancel: function () {
    wx.navigateBack({})
  },
  deleteAddress: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除该收货地址吗？',
      success: function (res) {
        if (res.confirm) {
          const db = wx.cloud.database()
        
          db.collection('useraddress').doc(id).remove({
            
            success: function(res) {
              wx.navigateBack({
                delta: 1,
              })
            }
          })
          
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  ,
  //获取地址
  getAddress: function() {

    var that = this;
    //先判断是否已授权
    wx.getSetting({
      
      success(res) {
        console.log(res.authSetting['scope.userLocation'])
        if (res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success: function () {
              //授权位置
                  wx.chooseLocation	({
                    success: function (res) {
                   console.log(res)
                      app.globalData.map_address=res.address
                      that.setData({
                        ['addressData.address'] :res.name+'(' +res.address+')'
                      }) 
                    
                    }
                  })
                   //授权位置结束
            },
          })
        }else{
          wx.showModal({
            title: '错误',
            content: '请打开又上角小程序设置，修改为允许使用位置信息！',
            showCancel: false
          })
        return
        }
      }
    })
  },
})